package com.zzyl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zzyl.base.ResponseResult;
import com.zzyl.constant.CacheConstant;
import com.zzyl.dto.ResourceDto;
import com.zzyl.entity.Resource;
import com.zzyl.service.ResourceService;
import com.zzyl.utils.StringUtils;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.MenuVo;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description ResourceController
 * @Author chenchuang
 * @Date 2024-08-31
 */
@RestController
@RequestMapping("/resource")
@Api(tags = "资源管理")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/list")
    @ApiOperation("分页查询资源列表")
    public ResponseResult<List<ResourceVo>> resourceList(@RequestBody ResourceDto dto) {
        //查询redis
        String listStr = (String) redisTemplate.opsForValue().get(CacheConstant.RESOURCE_LIST+"::"+dto.hashCode());
        //如果redis中有数据，则直接返回
        if (StringUtils.isNotEmpty(listStr)) {
            //把字符串转成集合
            List<Resource> resourceList = JSONArray.parseArray(listStr, Resource.class);
            return ResponseResult.success(resourceList);
        }
        //表示redis中没有命中
        List<ResourceVo> list = resourceService.findResourceList(dto);
        //同步数据到redis中一份
        redisTemplate.opsForValue().set(CacheConstant.RESOURCE_LIST+"::"+dto.hashCode(), JSON.toJSONString(list));
        return ResponseResult.success(list);
    }

    @PostMapping("/tree")
    @ApiOperation(value = "资源树形", notes = "资源树形")
    public ResponseResult<TreeVo> resourceTreeVo(@RequestBody ResourceDto resourceDto) {
        TreeVo treeVo = resourceService.resourceTreeVo(resourceDto);
        return ResponseResult.success(treeVo);
    }

    @PutMapping
    @ApiOperation(value = "资源添加", notes = "资源添加")
    public ResponseResult<ResourceVo> createResource(@RequestBody ResourceDto resourceDto) {
        resourceService.createResource(resourceDto);
        return ResponseResult.success();
    }

    @DeleteMapping("/{menuId}")
    @ApiOperation("删除资源菜单")
    public ResponseResult deleteResource(@PathVariable String menuId) {
        resourceService.deleteByMenuId(menuId);
        return ResponseResult.success();
    }

    @PatchMapping
    @ApiOperation("资源修改")
    public ResponseResult updateResource(@RequestBody ResourceDto resourceDto) {
        resourceService.update(resourceDto);
        return ResponseResult.success();
    }

    @PostMapping("/enable")
    @ApiOperation(value = "资源启用", notes = "资源启用")
    public ResponseResult enable(@RequestBody ResourceDto resourceDto) {
        resourceService.updateState(resourceDto);
        return ResponseResult.success();
    }
    @GetMapping("/menus")
    @ApiOperation("根据用户id查询菜单")
    public ResponseResult<List<MenuVo>> menus() {
        Long userId = UserThreadLocal.getMgtUserId();
        List<MenuVo> menus = resourceService.menus(userId);
        return ResponseResult.success(menus);
    }
}
